package net.xdclass.xdclass_shiro.dao;

import net.xdclass.xdclass_shiro.domain.Role;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 角色数据操作
 *
 * @author zhangsj
 * @date 2020/5/2 9:59
 */
public interface RoleMapper {

    /**
     * 查询用户所有角色
     *
     * @param userId 用户id
     * @return {@link Role}
     */
    @Select("select xr.id, xr.name, xr.deacription\n" +
            "from xd_user_role xur\n" +
            "left join xd_role xr on xr.id = xur.role_id\n" +
            "where xur.user_id = #{userId}")
    @Results(
            value = {
                    @Result(id = true, property = "id", column = "id"),
                    @Result(property = "name", column = "name"),
                    @Result(property = "deacription", column = "deacription"),
                    @Result(property = "permissionList", column = "id",
                            many = @Many(select = "net.xdclass.xdclass_shiro.dao.PermissonMapper.findListByRoleId", fetchType = FetchType.DEFAULT))
            }
    )
    List<Role> findListByUserId(@Param("userId") long userId);
}
